package com.ruiaa.meeting.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.ruiaa.meeting.common.CommonResponse;
import com.ruiaa.meeting.common.Constant;
import com.ruiaa.meeting.common.ResponseCode;
import com.ruiaa.meeting.model.MeetingRoom;
import com.ruiaa.meeting.model.User;
import com.ruiaa.meeting.service.MeetingRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

@RestController
@RequestMapping("/meetingroom")
@Validated
public class MeetingRoomController {

    @Autowired
    private MeetingRoomService meetingRoomService;

    //开放会议室
    @PutMapping("/open/{roomId}")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<MeetingRoom> openMeetingRoom(
            @PathVariable Integer roomId
    ) {
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return meetingRoomService.openMeetingRoom(roomId);
    }

    //关闭会议室
    @PutMapping("/close/{roomId}")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<MeetingRoom> closeMeetingRoom(
            @PathVariable Integer roomId
    ) {
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return meetingRoomService.closeMeetingRoom(roomId);
    }

    //查看所有会议室信息
    @GetMapping("/rooms")
    @SaCheckRole(Constant.Role.USER)
    public CommonResponse<List<MeetingRoom>> getAllMeetingRooms() {
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return meetingRoomService.getAllMeetingRooms();
    }

    //通过会议室号查看会议室信息
    @GetMapping("/room/{roomId}")
    @SaCheckRole(Constant.Role.USER)
    public CommonResponse<MeetingRoom> getMeetingRoomById(
            @PathVariable Integer roomId
    )
    {
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return meetingRoomService.getMeetingRoomById(roomId);
    }

    //添加会议室
    @PostMapping("/room")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<Object> addMeetingRoom(
            @RequestBody @Valid MeetingRoom meetingRoom
    ){
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return meetingRoomService.addMeetingRoom(meetingRoom);
    }

    //删除会议室
    @DeleteMapping("/room/{roomId}")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<Object> deleteMeetingRoomById(
            @PathVariable Integer roomId
    ){
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return meetingRoomService.deleteMeetingRoomById(roomId);
    }

    //修改会议室信息
    @PutMapping("/room/{roomId}")
    @SaCheckRole(Constant.Role.ADMIN)
    public CommonResponse<MeetingRoom> updateMeetingRoom(
            @PathVariable Integer roomId,
            @RequestParam @NotNull (message = "会议室id不能为空")Integer state,
            @RequestParam @NotNull (message = "会议室id不能为空")Integer number,
            @RequestParam @NotNull (message = "会议室id不能为空")String roomName
    ){
        String username=(String) StpUtil.getLoginId();
        User user=(User)StpUtil.getSession().get(username);
        if(user == null){
            return CommonResponse.createForError(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getMessage());
        }
        return meetingRoomService.updateMeetingRoom(roomId,state,number,roomName);
    }

}
